package com.hliushi.event.web;

import com.hliushi.event.entity.SysLogEvent;
import com.hliushi.event.entity.dto.OptLogDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author llhuang10
 * @date 2021/8/25 9:31
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private ApplicationContext applicationContext;


    @GetMapping("/get-user")
    public String getUser() {
        // 构造操作日志信息
        OptLogDTO logInfo = new OptLogDTO();
        logInfo.setRequestIp("127.0.0.1");
        logInfo.setUserName("admin");
        logInfo.setType("OPT");
        logInfo.setDescription("查询用户信息");

        // 构造事件对象
        ApplicationEvent event = new SysLogEvent(logInfo);

        // 发布事件
        applicationContext.publishEvent(event);

        long id = Thread.currentThread().getId();
        log.info("==========发布事件, 线程id: {}", id);
        return "OK";
    }
}
